﻿$(function(){
			$('#resources').datagrid({
				title:'菜单管理',
				url:'list.htm',
				type: "get",
	            datatype: "json",
				sortName: 'id',
				sortOrder: 'desc',
				remoteSort: false,
				idField:'id',
		        pagination: true,
		        rownumbers: true,
		        fitColumns: true,
				frozenColumns:[[
	                {field:'id',checkbox:true}
				]],
				columns:[[
			        {field:"name",title:'名称',width: 100},
			        {field:"icon",title:'图标',width: 120},
			        {field:"type",title:'类型',width: 120},
			        {field:"parent",title:'上级菜单',width: 120},
			        {field:"createTime",title:'添加日期',width: 150, formatter:formatterdate},
			        {field:"url",title:'地址',width: 450}
			        ]
				],
				toolbar:[{
					id:'add',
					text:'添加',
					iconCls:'icon-add',
					handler:function(){
						$("#rid").val("");
						$('#add-resource').window({  
						    width:450,  
						    height:400,
						    title:"添加菜单",
						    modal:true,
						    collapsible:false,
						    minimizable:false,
						    maximizable:false
						}).window("open");
						$("#pid").combobox({
							url:'menuList.htm',
							valueField:'id',
							textField:'name'
						});
						clearForm();
					}
				},{
					id:'edit',
					text:'修改',
					iconCls:'icon-edit',
					handler:function(){
						var rows = $('#resources').datagrid('getSelections');
						if(rows==null||rows.length==0){
							$.messager.alert('系统提示','请选择要修改的菜单!','warning');
						}else if(rows.length>1){
							$.messager.alert('系统提示','一次只能修改一个菜单!','warning');
						}else if(rows.length=1){
							$('#add-resource').window({  
							    width:450,  
							    height:400,  
							    modal:true,
							    title:"修改菜单",
							    collapsible:false,
							    minimizable:false,
							    maximizable:false,
							    onOpen:function(){
							    	$("#saveResource").form('load',{
										rid:rows[0].id,
										name:rows[0].name,
										icon:rows[0].icon,
										url:rows[0].url
									});
							    }
							}).window("open");
							$("#pid").combobox({
								url:'menuList.htm',
								valueField:'id',
								textField:'name'
							});
						}
					}
				},'-',{
					id:'remove',
					text:'删除',
					iconCls:'icon-remove',
					handler:function(){
						var rows = $('#resources').datagrid('getSelections');
						if(rows==null||rows.length==0){
							$.messager.alert('系统提示','请选择要删除的菜单!','warning');
						}else{
							$.messager.confirm('系统提示', '确定要删除菜单?', function(r){
								if(r){
									var ids = [];
									for(var i=0;i<rows.length;i++){
										ids.push(rows[i].id);
									}
									rid = ids.join('=');
									$.ajax({
										  type: 'POST',
										  url: "delete.htm?rids="+rid,
										  success: function(data){
											  $.messager.alert('系统提示','删除成功!','info');
											  reload(); 
										  },
										  error:function(data){
											  $.messager.alert('系统提示','删除失败!','warning');
										  }
										});
									}
							});
						}
					}
				}]
			});
			var p = $('#resources').datagrid('getPager');
			$(p).pagination({
				    pageSize:50,
				    pageList:[30,40,50,60,80]
			});
			function formatterdate(val, row) {
                var date = new Date(val);
                return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()+" "+date.getHours()+":"+date.getMinutes();
			}
			$("#save").click(function(){
				$("#saveResource").form('submit',{
					url:"save.htm",  
				    onSubmit: function(){  
				    	return $(this).form('validate');
				    },  
				    success:function(data){  
				    	$.messager.alert('系统提示','保存成功!','question');
				    	$("#add-resource").window('close');
				    	reload(); 
				    }  
				});
			});
			$("#cannel").click(function(){
				$("#add-resource").window('close');
			});
			
			function clearForm(){
				$("#rid").val("");
				$("#name").val("");
				$("#url").val("");
				$("#icon").val("");
			}
			function reload(){
				$("#resources").datagrid('reload'); 
			}
			
});
		
		